package com.orhon.smartcampus.modules.base.mutation;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlInputModel.GQLSchoolInput;
import com.orhon.smartcampus.modules.base.inputMapper.SchoolInputMapper;
import com.orhon.smartcampus.modules.systemctl.dao.ModulesDao;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.user.dao.TokenService;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddSchoolMutation implements DataFetcher {


	@Autowired
	SchoolsDao schoolsDao;


	@Autowired
	TokenService tokenService;

	@Autowired
	ModulesDao modulesDao;


	/**
	 * 新增/修改学校信息
	 * editor : ths
	 */
	@Override
	public Object get(DataFetchingEnvironment environment) throws Exception {

		//String token = (String) SecurityUtils.getSubject().getPrincipal();
		String token = tokenService.getSystemToken();
		Long schoolId = JwtUtils.getSchoolId(token);

		GQLSchoolInput input = SchoolInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

		Schools schools = null;

		if (input.getId() != null) {
			schools = schoolsDao.get(Schools.class, input.getId());
			schools.setUpdated_at(new Date());
		} else {
			schools = new Schools();
			schools.setCreated_at(new Date());
		}
		if(input.getSchool_name() != null){

			schools.setSchool_name(JSON.toJSONString(input.getSchool_name()));
		}
		if(input.getSchool_order() != null){

			schools.setSchool_order(input.getSchool_order());
		}
		if(input.getSchool_number() != null){

			schools.setSchool_number(input.getSchool_number());
		}
		if(input.getIntroduction() != null){

			schools.setIntroduction(JSON.toJSONString(input.getIntroduction()));
		}
		if(input.getIcon() != null){

			schools.setIcon(input.getIcon());
		}
		if(input.getProvince_id() != null){

			schools.setProvince_id(input.getProvince_id());
		}
		if(input.getCity_id() != null){

			schools.setCity_id(input.getCity_id());
		}
		if(input.getDistrict_id() != null){

			schools.setDistrict_id(input.getDistrict_id());
		}
		if(input.getRegion_id() != null){

			schools.setRegion_id(input.getRegion_id());
		}
		if(input.getHeader_img() != null){

			schools.setHeader_img(input.getHeader_img());
		}
		if(input.getMain_color() != null){

			schools.setMain_color(input.getMain_color());
		}
		if(input.getMark() != null){

			schools.setMark(input.getMark());
		}
		if(input.getSchool_type() != null){

			schools.setSchool_type(input.getSchool_type());
		}
		if(input.getCloud_status() != null){

			schools.setCloud_status(input.getCloud_status());
		}
		if(input.getClient_key() != null){

			schools.setSchool_client_key(input.getClient_key());
		}
		if(input.getClient_secret() != null){

			schools.setClient_secret(input.getClient_secret());
		}
		if(input.getOrhonedu_base() != null){

			schools.setOrhonedu_base(input.getOrhonedu_base());
		}
		if(input.getLng() != null){

			schools.setLng(input.getLng());
		}
		if(input.getLat() != null){

			schools.setLat(input.getLat());
		}
		if(input.getAddress() != null){

			schools.setAddress(input.getAddress());
		}
		//维护学习模块
		if(input.getModule_id()!=null) {
			ArrayList<Modules> list = new ArrayList<Modules>();
			List<Integer> ids = input.getModule_id();
			for (Integer id : ids) {
				Modules modules = modulesDao.findById(Long.valueOf(id));
				list.add(modules);
			}
			schools.setModules(list);
		}
		if (schoolId != null) {
			Schools parent = schoolsDao.get(Schools.class, schoolId);
			schools.setSchools(parent);
		}
		try {
			schoolsDao.saveOrUpdate(schools);
		} catch (Exception e) {
			return Result.error(500, "" + e);
		}
		return Result.ok();
	}
}

